// pages/newState/newState.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    src: [],
    url: [],
    picBtn: true,
    isActive: false,
    dirty:[],
  },
  gotoShow: function() {
    var that = this
    var photoNum = 3 - that.data.src.length;
    wx.chooseImage({
      count: photoNum, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {

        for (var i = 0; i < res.tempFilePaths.length; i++) {

          app.uploadToQiNiu({
            filePath: res.tempFilePaths[i],
            success: function(data) {
              that.data.url = that.data.url.concat(data.imageURL)
              if (that.data.url.length) {
                that.toggle('isActive', true)
              }
              that.setData({
                src: that.data.src.concat(data.imageURL)
              })
              if (that.data.src.length >= 3) {
                that.toggle('picBtn', false)
              }
            }
          });
        }

      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  // 移除图片
  deleteImg: function(e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    console.log(dataset.index)
    that.data.src.splice(dataset.index, 1)
    that.data.url.splice(dataset.index, 1)
    that.toggle('src', that.data.src)
    that.toggle('url', that.data.url)
    if (that.data.content || that.data.url.length) {
      this.toggle('isActive', true)
    } else {
      this.toggle('isActive', false)
    }
    that.toggle('picBtn', true)
  },
  bindinput: function(e) {
    this.toggle('content', e.detail.value)
    this.widthCheck(e.detail.value, 1000)
    if (e.detail.value || this.data.url.length) {
      this.toggle('isActive', true)
    } else {
      this.toggle('isActive', false)
    }
  },
  toSend: function() {
    var that = this
    if (!this.data.content.trim()){
      wx.showToast({
        title: '内容不能为空',
        icon: "none"
      })
      return
    }
    if (that.data.isActive || that.data.url.length) {
      if (that.data.content != '') {
        var n = 0
        // console.log('词库2', this.data.dirty.length)
        for (var i = 0; i < this.data.dirty.length; i++) {
          // console.log('循环')
          if (that.data.content.indexOf(this.data.dirty[i]) >= 0) {
            console.log('查询2')
            wx.showToast({
              title: '您输入的内容有违禁词，敏感词已被替换成星号,请编辑后重新提交',
              icon: 'none',
              duration: 2000
            })
            var fw = this.data.dirty[i]
            var fw1 = ''
            for (var k = 0; k < fw.length; k++) {
              fw1 = fw1 + '*'
            }
            var activityProcess = that.data.content.replace(fw, fw1)
            that.setData({
              content: activityProcess
            });
            console.log('替换', that.data.content)
            // console.log('替换1', activityProcess)
            n = n + 1
          }
        }
        if (n > 0) {
          return
        }
      }
      if (that.data.type){
        var url = "orgmini/home/notice/addNewComment"
        var data = {
          "messageId": that.data.messageId,
          "content": that.data.content,
          "pictureList": that.data.url,
          "type":2
        }
      }else{
        var url = "orgmini/notice/saveComment"
        var data = {
          "messageId": that.data.messageId,
          "content": that.data.content,
          "pictureList": that.data.url
        }
      }
      // 发送动态
      app.req({
        url: url,
        method: "post",
        data:data,
        success: function(res) {
          if (res.code == 200) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 2000
            })
          }
        }
      });
    }
  },
  // 输入框长度字节限制
  widthCheck(str, maxLen) {
    var that = this;
    var w = 0;
    var tempCount = 0;
    //length 获取字数数，不区分汉子和英文 
    for (var i = 0; i < str.length; i++) {
      //charCodeAt()获取字符串中某一个字符的编码 
      var c = str.charCodeAt(i);
      //单字节加1 
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        w++;
      } else {
        w += 2;
      }
      if (w > maxLen) {
        str = str.substr(0, i);
        that.toggle('content', str)
        break;
      }
    }
  },
  getdirty() {
    var that = this
    app.req({
      url: "wechat/dirty/all",
      method: "post",
      success: function (res) {
        // console.log('脏字库')
        if (res.code == 200) {
          wx.setStorageSync('dirty', res.data);
          that.setData({
            dirty: res.data
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toggle('messageId', options.id)
    if (options.type){
      this.toggle('type', options.type)
    }
    if (wx.getStorageSync('dirty') == undefined || wx.getStorageSync('dirty') == '') {
      this.getdirty()
      // console.log('无词库')
    } else {
      // console.log('有词库', wx.getStorageSync('dirty'))
      this.setData({
        dirty: wx.getStorageSync('dirty')
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})